package middleware

import (
	"github.com/gin-gonic/gin"
)

type (
	Skipper func(ctx *gin.Context) bool
)

func AllowedPathPrefixSkipper(prefixes ...string) Skipper {
	return func(ctx *gin.Context) bool {
		path := ctx.Request.URL.Path
		pathLen := len(path)

		for _, p := range prefixes {
			if pl := len(p); pathLen >= pl && path[:pl] == p {
				return true
			}
		}
		return false
	}
}

func EmptyMiddleware() gin.HandlerFunc {
	return func(ctx *gin.Context) {
		ctx.Next()
	}
}

func SkipHandler(ctx *gin.Context, skippers ...Skipper) bool {
	for _, skipper := range skippers {
		if skipper(ctx) {
			return true
		}
	}
	return false
}

func GetRequestIDFromRequest(ctx *gin.Context) string {
	return ctx.GetHeader(RequestIDHeaderKey)
}
